package xyz.xtt.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.xtt.admin.entity.TblCarouseImage;
import xyz.xtt.admin.request.CarouseImageRequest;
import xyz.xtt.admin.service.ICarouseImageService;
import xyz.xtt.admin.utils.PageUtilV2;
import xyz.xtt.admin.vo.CarouseImageVo;
import xyz.xtt.common.response.ApiResult;

import java.util.List;
import java.util.Map;

/**
 * @author xugaopeng
 * @date 2024-12-11-16:50
 */
@RestController
@RequestMapping("/api/admin/carouse_image")
public class CarouseImageController {
    @Autowired
    private ICarouseImageService service;

    @Autowired
    private PageUtilV2 pageUtil;

    @GetMapping("/list")
    public ApiResult<Page<CarouseImageVo>> getList(CarouseImageRequest request) {
        Integer current = pageUtil.getCurrent(request.getCurrent());
        request.setCurrent(current);
        Integer pageSize = pageUtil.getPageSize(request.getPageSize());
        request.setPageSize(pageSize);
        return ApiResult.ok(service.getList(request));
    }

    @PostMapping("/add")
    public ApiResult addCarouseImages(CarouseImageRequest imageRequest) {
        TblCarouseImage build = TblCarouseImage.builder().title(imageRequest.getTitle()).build();
        service.addCarouseImage(build, imageRequest.getFile());
        return ApiResult.ok();
    }

    @PostMapping("/del")
    public ApiResult delCarouseImages(@RequestBody Map<String, Object> ma) {
        service.delCarouseImages((List) ma.get("ids"));
        return ApiResult.ok();
    }

    @PostMapping("/update")
    @ResponseBody
    public ApiResult updateCarouse(CarouseImageRequest imageRequest) {
        TblCarouseImage build = TblCarouseImage.builder().id(imageRequest.getId()).title(imageRequest.getTitle()).build();
        service.updateCarouse(build, imageRequest.getFile());
        return ApiResult.ok();
    }
}
